<template>
  <BasicModal
    v-bind="$attrs"
    @register="registerModal"
    width="40%"
    :title="getTitle"
    @ok="handleSubmit"
  >
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button
          type="primary"
          v-if="hasPermission(['sysSequenceSub:add'])"
          @click="handleCreate"
        >
          新增
        </a-button>
      </template>

      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              auth: 'sysSequenceSub:edit',
              tooltip: '编辑',
              onClick: handleEdit.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              auth: 'sysSequenceSub:delete',
              color: 'error',
              tooltip: '删除',
              popConfirm: {
                title: '是否确认删除',
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <EditSubModal
      ref="modal"
      @register="registerEditSubModal"
      @success="handleSuccess"
    />
  </BasicModal>
</template>
<script lang="ts">
import { defineComponent, computed, ref, unref } from 'vue';
export default defineComponent({
  name: 'SubListModal',
});
</script>
<script lang="ts" setup>
import { usePermission } from '@/hooks/web/usePermission';
import { BasicTable, useTable, TableAction } from '@/components/Table';
import { page, del } from '@/api/sys/dev/sysSequenceSubManage';
import { BasicModal, useModal, useModalInner } from '@/components/Modal';
import EditSubModal from './EditSubModal.vue';
import { useMessage } from '@/hooks/web/useMessage';
import { subColumns } from './data';
const { createMessage } = useMessage();
const [registerEditSubModal, { openModal }] = useModal();
const { hasPermission } = usePermission();
const configId = ref();
const [registerTable, { reload }] = useTable({
  api: page,
  columns: subColumns,
  useSearchForm: false,
  showTableSetting: true,
  bordered: true,
  showIndexColumn: false,
  immediate: false,
  actionColumn: {
    width: 120,
    title: '操作',
    dataIndex: 'action',
    slots: { customRender: 'action' },
    fixed: 'right',
  },
  beforeFetch: (params) => {
    params.configId = configId.value;
  },
});
let isUpdate = ref(true);
const getTitle = computed(() => (!unref(isUpdate) ? '新增' : '编辑'));
const [registerModal, { setModalProps, closeModal }] = useModalInner(
  async (data) => {
    setModalProps({ confirmLoading: false });
    isUpdate.value = !!data?.isUpdate;
    configId.value = data.record.id;
    console.log(data.record.id, 'dada');
    reload({
      searchInfo: {
        configId: data.record.id,
      },
    });
  },
);
function handleCreate() {
  openModal(true, {
    isUpdate: false,
    record: {
      configId: configId.value,
    },
  });
}
function handleEdit(record: Recordable) {
  openModal(true, {
    record: { ...record, configId: configId.value },
    isUpdate: true,
  });
}
function handleDelete(record: Recordable) {
  del([record.id]).then(() => {
    createMessage.success(`删除成功`);
    reload();
  });
}
function handleSubmit() {
  closeModal();
}
function handleSuccess() {
  reload();
}
</script>
